﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Roslyn.Compilers;
using Roslyn.Compilers.CSharp;
using Roslyn.Compilers.Common;
using Roslyn.Scripting;
using Roslyn.Scripting.CSharp;
namespace RoslynEval
{
    public class ScriptEval
    {
        ScriptEngine scriptengine = new ScriptEngine();

        public Double Eval(String inEquation)
        {
            inEquation = inEquation.Replace("Math", "System.Math");
            object obj = scriptengine.Execute(inEquation);
            return Convert.ToDouble(obj);
        }

        public static Double Evaluate(String inEquation)
        {
            ScriptEngine scriptengine = new ScriptEngine();
            inEquation = inEquation.Replace("Math","System.Math");
            object obj;
            try
            {
                obj = scriptengine.Execute(inEquation);
            }
            catch
            {
                return Double.NaN;
            }
            return Convert.ToDouble(obj);
        }

        //public static Double[] Evaluate(String[] inEquations)
        //{
        //    ScriptEngine scriptengine = new ScriptEngine();
        //    Int32 L;
        //    Double[] lOut = new double[inEquations.Length];
        //    object obj;
        //    for (L = 0; L < inEquations.Length; L++)
        //    {
        //        inEquations[L] = inEquations[L].Replace("Math", "System.Math");
        //        obj = scriptengine.Execute(inEquations[L]);
        //        lOut[L] = Convert.ToDouble(obj);
        //    }
        //    return lOut;
        //}
    }
}
